package com.j.thetabk.thetas;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by j on 2016/4/14.
 */
public class ThetaSPropertyA extends ThetaSProperty{

    public boolean []selection=null;
    protected int checkedNum;

    public ThetaSPropertyA()
    {
        super();
        selection=null;
        checkedNum=0;
    }

    public String DisplayChoice(int index)
    {
        if(selection!=null && checkedNum>1)
            return super.DisplayChoice(index)+"["+checkedNum+"]";
        return super.DisplayChoice(index);
    }

    public void SyncSelection()
    {
        selection=new boolean[support.length()];
        if(support.length()<1)
        {
            checkedNum=-1;
            return;
        }
        for(boolean tmp:selection)
            tmp=false;
        selection[choice]=true;
        checkedNum=1;
    }

    public void ParseChoiceAndSupport(int index, JSONObject jbProperties) throws JSONException {
        super.ParseChoiceAndSupport(index,jbProperties);
        if(selection!=null)
            SyncSelection();
    }

    public static int FirstTrue(boolean chk[])
    {
        if(chk==null || chk.length<1)
            return -1;

        for (int i=0;i<chk.length;i++)
        {
            if(chk[i])
                return i;
        }
        return -2;
    }

    public int FirstTrue() {
        return FirstTrue(selection);
    }


    public static int TrueCount(boolean chk[])
    {
        if(chk==null)
            return -1;
        int i=0;
        for(boolean achk:chk)
        {
            if(achk)
                i++;
        }
        return i;
    }

    public void TrueCount() {
        checkedNum = TrueCount(selection);
    }



    public static int BracketList(boolean chk[], ArrayList<Integer> bklist)
    {
        bklist.clear();
        if(chk==null)
            return 0;
        for(int i=0;i<chk.length;i++)
        {
            if(chk[i])
                bklist.add(i);
        }
        return bklist.size();
    }

    public int BracketList(ArrayList<Integer> bklist)
    {
        return BracketList(selection, bklist);
    }

    public static void AddList(ArrayList<Integer> l, ArrayList<Integer> a,
                               ArrayList<Integer> la, int mark[], boolean flip[])
    {
        int lan=la.size();
        if(lan<2)
            return;
        if(flip[0])
        {
            for(int j=lan-2;j>=0;j--)
            {
                l.add(la.get(j));
                a.add(mark[0]);
            }
        }
        else
        {
            for(int j=1;j<lan;j++)
            {
                l.add(la.get(j));
                a.add(mark[0]);
            }
        }
        flip[0]=!flip[0];
    }

    public static void AddList(ArrayList<Integer> l, ArrayList<Integer> ab,
                               ArrayList<Integer> la, ArrayList<Integer> lb,
                               int mark[], boolean flip[])
    {
        AddList(l, ab, la, mark, flip);
        int lbn=lb.size();
        if(lbn<2)
            return;
        if(flip[1])
        {
            for(int j=lbn-2;j>=0;j--)
            {
                l.add(lb.get(j));
                ab.add(mark[1]);
                AddList(l, ab, la, mark, flip);
            }
        }
        else
        {
            for(int j=1;j<lbn;j++)
            {
                l.add(lb.get(j));
                ab.add(mark[1]);
                AddList(l, ab, la, mark, flip);
            }
        }
        flip[1]=!flip[1];
    }

    public static void AddList(ArrayList<Integer> l, ArrayList<Integer> abc,
                               ArrayList<Integer> la, ArrayList<Integer> lb, ArrayList<Integer> lc,
                               int mark[], boolean flip[])
    {
        AddList(l,abc,la,lb,mark,flip);
        int lcn=lc.size();
        if(lcn<2)
            return;
        if(flip[2])
        {
            for(int j=lcn-2;j>=0;j--)
            {
                l.add(lc.get(j));
                abc.add(mark[2]);
                AddList(l,abc,la,lb,mark,flip);
            }
        }
        else
        {
            for(int j=1;j<lcn;j++)
            {
                l.add(lc.get(j));
                abc.add(mark[2]);
                AddList(l,abc,la,lb,mark,flip);
            }
        }
        flip[2]=!flip[2];
    }








}
